package cn.gdcp.contentprovidetest;

import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Parcel;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.List;

public class ContentProviderTestActivity extends AppCompatActivity implements View.OnClickListener {
       Button queryBtn;
       Button deleteBtn;
       Button addBtn;
       Button upGradeBtn;
       ContentResolver resolver;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_provider_test);
        initButtonView();
        initButtonOnclick();
         resolver=getContentResolver();
    }

    private void initButtonOnclick() {
        queryBtn.setOnClickListener(this);
        addBtn.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
        upGradeBtn.setOnClickListener(this);
    }

    private void initButtonView() {
        queryBtn=findViewById(R.id.query_button);
        addBtn=findViewById(R.id.add_button);
        upGradeBtn=findViewById(R.id.upGrade_button);
        deleteBtn=findViewById(R.id.delete_button);
    }

    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_button:
                addData();
                break;
            case R.id.delete_button:
                deleteData();
                break;
            case R.id.query_button:
                queryData();
                break;
            case R.id.upGrade_button:
                upGradeData();
                break;
            default:
                break;
        }
    }

    private void upGradeData() {
         Uri uri=Uri.parse("content://cn.gdcp.sqlitetest.provider/book");
            ContentValues values=new ContentValues();
              values.put("name","陆逊");
              resolver.update(uri,values,"name=?",new String[]{"陆秀夫"});
    }

    private void deleteData() {
        Uri uri=Uri.parse("content://cn.gdcp.sqlitetest.provider/book/11");
         resolver.delete(uri,null,null);
    }

    private void addData() {
        Uri uri=Uri.parse("content://cn.gdcp.sqlitetest.provider/book");
        ContentValues values=new ContentValues();
        values.put("name","陆经东");
         resolver.insert(uri,values);


    }

    private void queryData() {
        Uri uri=Uri.parse("content://cn.gdcp.sqlitetest.provider/book");
        Cursor cursor=resolver.query(uri,null,null,null,null);
          if (cursor!=null){
              while(cursor.moveToNext()){
                 String name=cursor.getString(cursor.getColumnIndex("name"));
                 String id=String.valueOf(cursor.getInt(cursor.getColumnIndex("id")));
                 Log.e("ContentProvider",name);
                  Log.e("ContentProvider",id);
                  }
              cursor.close();

          }
    }
}
